//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
#include "StringUtilities.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <fstream>
#include <iostream>
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
using namespace std;
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
StringUtilities::StringUtilities () { }
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
StringUtilities::StringUtilities (const StringUtilities& stringUtilities) { }
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
StringUtilities::StringUtilities (const StringUtilities* stringUtilities) { }
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
char* StringUtilities::GetStringInPosition (char* text, int position, char* result) {
  
  if (position < 0 || !text)
    return NULL;
  
  char* parts        = NULL;
  char  textPointer [strlen(text) + 1];
  int   counter      = 0;
  
  strcpy(textPointer, text);
  parts = strtok(textPointer, " \n\t\r");
  
  while (parts) {
    
    if (++counter == position)
      break;
    
    parts = strtok(NULL, " \n\t\r");
    
  }
  
  if (result)
    delete result;
  
  if (parts) {
    result = new char [strlen(parts) + 1];
    strcpy(result, parts);
    result[strlen(result)] = '\0';
  }
  
  return result;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
float StringUtilities::GetFloatInPosition (char* text, int position) {
  
  if (position < 0 || !text)
    return 0;
  
  char* parts        = NULL;
  char  textPointer [strlen(text) + 1];
  int   counter      = 0;
  
  strcpy(textPointer, text);
  parts = strtok(textPointer, " \n\t\r");
  
  while (parts) {
    
    if (++counter == position)
      break;
    
    parts = strtok(NULL, " \n\t\r");
    
  }
  
  return (float) atof(parts);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
double StringUtilities::GetDoubleInPosition (char* text, int position) {
  
  if (position < 0 || !text)
    return 0;
  
  char* parts        = NULL;
  char  textPointer [strlen(text) + 1];
  int   counter      = 0;
  
  strcpy(textPointer, text);
  parts = strtok(textPointer, " \n\t\r");
  
  while (parts) {
    
    if (++counter == position)
      break;
    
    parts = strtok(NULL, " \n\t\r");
    
  }
  
  return (double) atof(parts);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int StringUtilities::GetIntInPosition (char* text, int position) {
  
  if (position < 0 || !text)
    return 0;
  
  char* parts        = NULL;
  char  textPointer [strlen(text) + 1];
  int   counter      = 0;
  
  strcpy(textPointer, text);
  parts = strtok(textPointer, " \n\t\r");
  
  while (parts) {
    
    if (++counter == position)
      break;
    
    parts = strtok(NULL, " \n\t\r");
    
  }
  
  return (int) atoi(parts);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
long StringUtilities::GetLongInPosition (char* text, int position) {
  
  if (position < 0 || !text)
    return 0;
  
  char* parts        = NULL;
  char  textPointer [strlen(text) + 1];
  int   counter      = 0;
  
  strcpy(textPointer, text);
  parts = strtok(textPointer, " \n\t\r");
  
  while (parts) {
    
    if (++counter == position)
      break;
    
    parts = strtok(NULL, " \n\t\r");
    
  }
  
  return (long) atoi(parts);
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
int StringUtilities::CountNumberString (char* text) {
  
  if (!text)
    return -1;
  
  char* parts        = NULL;
  char  textPointer [strlen(text) + 1];
  int   counter      = 0;
  
  strcpy(textPointer, text);
  parts = strtok(textPointer, " \n\t\r");
  
  while (parts) {
    counter++;
    parts = strtok(NULL, " \n\t\r");
  }
  
  return counter;
  
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
//---------------------------------------------------------------------------------------------------------------------------------------------------------//
